const util = require('utils/util.js');
App({
  onLaunch: function() {
    var a = wx.getStorageSync('logs') || [];
    a.unshift(Date.now()), wx.setStorageSync('logs', a), wx.getSetting({
      success: b => {
        b.authSetting['scope.userInfo'] && wx.getUserInfo({
          success: c => {
            this.globalData.userInfo = c.userInfo, this.userInfoReadyCallback && this.userInfoReadyCallback(c)
          }
        })
      }
    })
  },

  globalData: {
    userInfo: null,
    url: 'https://app.gzfcard.com/WebAPI',
    // url: 'http://192.168.1.90:9066/WebAPI', 
    code: '',
    headerCookie: '',
    cardid: '',
    qrcodeRefres: !1,
    company: 'FCARD',
    // aAppId: '你的APPID',//必填（）
    // secret: '你的AppSecret'//必填（）
  },

  getHttpData(a, b, c, d) {
    let e = {};
    '[object String]' === Object.prototype.toString.call(d) ? (e['content-type'] = 'application/x-www-form-urlencoded', e.fcard = Math.round(new Date().getTime() / 1e3), e.Cookie = 'ASP.NET_SessionId=' + d) : (e['content-type'] = 'application/x-www-form-urlencoded', e.fcard = Math.round(new Date().getTime() / 1e3));
    // e.AppId = this.globalData.aAppId, 
    // e.secret = this.globalData.secret;
    let f = this;
    return new Promise((g, h) => {
      wx.request({
        url: a,
        data: b,
        header: e,
        method: c,
        dataType: 'json',
        responseType: 'text',
        success: function(i) {
          switch (i.header['Set-Cookie'] && (f.globalData.headerCookie = util.GetCookieJson(i.header['Set-Cookie'])[0]['ASP.NET_SessionId']), i.statusCode) {
            case 400:
              util.setTOut('\u9519\u8BEF\u8BF7\u6C42', 3e3);
              break;
            case 401:
              util.setTOut('\u672A\u6388\u6743\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55', 3e3);
              break;
            case 403:
              util.setTOut('\u62D2\u7EDD\u8BBF\u95EE', 3e3);
              break;
            case 404:
              util.setTOut('\u8BF7\u6C42\u9519\u8BEF,\u672A\u627E\u5230\u8BE5\u8D44\u6E90', 3e3);
              break;
            case 405:
              util.setTOut('\u8BF7\u6C42\u65B9\u6CD5\u672A\u5141\u8BB8', 3e3);
              break;
            case 408:
              util.setTOut('\u8BF7\u6C42\u8D85\u65F6', 3e3);
              break;
            case 500:
              util.setTOut('\u670D\u52A1\u5668\u7AEF\u51FA\u9519', 3e3);
              break;
            case 501:
              util.setTOut('\u7F51\u7EDC\u672A\u5B9E\u73B0', 3e3);
              break;
            case 502:
              util.setTOut('\u7F51\u7EDC\u9519\u8BEF', 3e3);
              break;
            case 503:
              util.setTOut('\u670D\u52A1\u4E0D\u53EF\u7528', 3e3);
              break;
            case 504:
              util.setTOut('\u7F51\u7EDC\u8D85\u65F6', 3e3);
              break;
            case 505:
              util.setTOut('http\u7248\u672C\u4E0D\u652F\u6301\u8BE5\u8BF7\u6C42', 3e3);
              break;
            default:
          }
          console.log(i);
          if (i.data.result==-1){
            console.log(i,'+++++++++++++++++++++++++++++++++++++')
            wx.navigateTo({
              url: '/pages/login/index',
            })
          }
          g(i)
        },
        fail: function(i) {
          console.log(i)
          console.log(i), h(i)
        }
      })
    })
  },
  Login(a) {
    let b = this;
    wx.login({
      success: c => {
        b.globalData.code = c.code, b.getHttpData(this.globalData.url + '/MicroApp/Login', {
          code: c.code
        }, 'post', b.globalData.headerCookie).then(d => {
          1 == d.data.result && (console.log('\u767B\u5F55\u6210\u529F'), a())
        }).catch(d => {
          'request:fail ' == d.errMsg && wx.showToast({
            title: '\u8BF7\u6C42\u8D85\u65F6\uFF01',
            icon: 'none',
            duration: 2e3
          }), console.log(d)
        })
      }
    })
  }
});